<div class="flow_chart">
    <div class="pos_x1 bg_a1" title="Kiểm tra đơn đặt hàng"></div>
    <div class="pos_x2 bg_b1" title="Kiểm tra thông tin nhận hàng và phương thức vận chuyển"></div>
    <div class="pos_x3 bg_c1" title="Thao tác tiếp"></div>
</div>


<?
	function order()
	{
		global $conn;
		global $adminEmail;
        global $order_id;
		$cust=$_SESSION['cust'];		
   		
        $tenkhachhang=$cust['name'] ;
		
	 	$phai=	$cust['sex'];
		$diachi= $cust['address'];
		$dienthoai=$cust['phone'];		
        $email=$cust['email'];
		$tieude=$cust['title'];
	    $noidung=$cust['content'];
		$thoidiemgiaohang=	$cust['date'];
	 	$diachigiaohang=	$cust['add_to'];
	 	$thoigiangiaohang=$cust['time'];
		if ($tenkhachhang==""||$diachi==""||$dienthoai==""||$email==""||$diachigiaohang=="")
		{
			echo "<script>window.location='./?frame=customer'</script>";
			return "Khong duoc";
		}
        $dathang="";
		$cart=$_SESSION['cart'];
	
        $tong=0;
		$tongcong=0;
		$cnt=0;
        $date_added=date("Y-m-d H:i:s");
        $md5_hash = md5(rand(0,999));
        $code = substr($md5_hash, 15, 5);
        $order_id=$code;
        $query = "INSERT INTO tbl_order (code,ten_kh,phai,dia_chi,dien_thoai,email,tieu_de,noidung,td_gh,dd_gh,tg_gh,date_added) 
        VALUES ('$code','$tenkhachhang','$phai','$diachi','$dienthoai','$email','$tieude','$noidung','$thoidiemgiaohang','$diachigiaohang','$thoigiangiaohang','$date_added')";
        $result1=mysql_query($query,$conn);
		foreach ($cart as $product){
		$sql = "select * from tbl_product where id='".$product[0]."'";
		$result = mysql_query($sql,$conn);
		if (mysql_num_rows($result)>0)
		{
		
		$pro=mysql_fetch_assoc($result);
		$masanpham=$pro['id'];
            $tensanpham=$pro['name'];
            $soluong=$product[1];
            $dongia=number_format($pro['price'],0,',','.');
            $thanhtien=$pro['price']*$product[1];
            $tong=$tong+$pro['price']*$product[1];
        $query = "SELECT id FROM tbl_order where code='$code'";
        $kq=mysql_query($query, $conn);
        $tong_soluong=$tong_soluong+$soluong;
        if (!$kq) 
        {
        echo "name is that bai";
        echo "<br />";
        }
        $row = mysql_fetch_row($kq);
		$query = "INSERT INTO tbl_order_detail (order_id,product_id,quantity,price) VALUES ('$row[0]','$masanpham','$soluong','$dongia')";
					$loigoi1= mysql_query($query, $conn);
					
		}
		$cnt=$cnt+1;
        
		}
	    $tong=number_format($tong,0,'.','.');
        
        
        $query_update="UPDATE tbl_order SET so_sanpham='$tong_soluong', tong_tien='$tong', last_modified='$date_added' WHERE code='$code'";
        $loigoi= mysql_query($query_update, $conn);
        if (!loigoi)
        {
            return "<p align='center' class='err'><font color=red>Không thể gửi thông tin !.</font></p>";
        }
        else
        {
            return "";
        }
		foreach ($cart as $product){
		  if (mysql_num_rows($result)>0)
          {
            
          
			$sql = "select * from tbl_product where id='".$product[1]."'";
			$result = mysql_query($sql,$conn);
			$pro=mysql_fetch_assoc($result);
			
			$dathang.="Ma san pham : ".$pro['code']."<br>"; 
            
            //$masanpham=$pro['code'];
            $masanpham=$pro['id'];
            $tensanpham=$pro['name'];
            $soluong=$product[1];
            $dongia=number_format($pro['price'],0,',','.');
            $thanhtien=$pro['price']*$product[1];
            $tong=$tong+$pro['price']*$product[1];// Tổng tiền khi thêm 1 sản phẩm chưa ra khỏi vòng for
            // vÌ vậy ở đây cần thực hiện câu lệnh thêm sản phẩm vào
             //$query = "insert into tbl_order_detail  (name,location,email,url,comments) 
             
             //values ('return mysql_query($query, $conn);$name', '$location', '$email', '$url', '$comments')";
                
         
			$dathang.="Ten san pham : ".$pro['name']."<br>"; 
			$dathang.="So luong : ".$product[1]."<br>"; 
			$dathang.="Don gia : ".number_format($pro['price'],0,',','.')."&nbsp;"."$"."<br>";
			$dathang.="Thanh tien : ".$pro['price']*$product[1]."&nbsp;"."$<br><br>";
			
			$tongcong=$tongcong+$pro['price']*$product[1]."&nbsp;"."$";
            }
           
			$cnt=$cnt+1;
		} 
        //Lúc này mới thực hiện tính tổng tiền lại để gởi đi.
		$dathang.="<hr>Tong cong : ".number_format($tongcong,0,',','.')."&nbsp;"."$<br>";
        $tong=number_format($tong,0,'.','.');
		//$m2=send_mail($email,$adminEmail, "Thong tin dat hang cua : ".$tenkhachhang, "Ho ten : ".$tenkhachhang."<br>Dia chi : ".$diachi."<br>Dien thoai : ".$dienthoai."<br>Email : ".$email."<BR><hr><b>Don hang :</b><br>".$dathang);
	//	 $query = "INSERT INTO tbl_order (order_id,product_id,quantity,price) VALUES ('150','$masanpham','$soluong','$dongia')";
            //return mysql_query($query, $conn);
		//if (m2)
		//{
			//return "Mã order của quý khách là: $code. Vui lòng lưu mã để tra cứu khi cần. Thanks!";
		//}
		//else
		//	return "Không thể gửi thông tin !. Mã order của quý khách là: $code. Vui lòng lưu mã để tra cứu khi cần. Thanks!";
	}

?>
<?	
	if (count($_SESSION['cart'])<=0) echo "<script>window.location='./?frame=cart'</script>";
	$cart=$_SESSION['cart'];
	
	$cust=$_SESSION['cust'];
?>    

<?
	if (isset($_POST['butSub']))
	{
		$tongcong=0;
		$cnt=0;
		
		if (order()=="")
        {           
			echo "<script>window.location='./?frame=checkout&code=1&order_id=$order_id'</script>";
        }
		else
			{
				echo "<p align='center' class='err'><font color=red>Không thể gửi thông tin !.</font></p>";
			}
	}
	
?>

<p align="center" style="line-height: 150%" class="err">
<font face="Tahoma" style="font-size: 18px; padding-top:20px">
<? 
	if ($_REQUEST['code']=='1') {
	    $order_code=$_REQUEST['order_id'];
   		echo "<span style='font:'Tahoma; font-size:18px; color:#000000>Thông tin đặt hàng của bạn đã được gửi tới chúng tôi</span><br /><br />";
        echo "<a href='./'><span style='font:'Tahoma; align='center' font-size:18px; color:#000000>Nhấn vào đây để trở về trang chủ !.</span></a><br/><br/>";
        echo "<p align='center' class='err'><font color=blue>Mã order của quý khách là: <b>$order_code</b> . Vui lòng lưu mã để tra cứu khi cần. Thanks!.</font></p>";
   		
		unset($_SESSION['cart']);
	}
	else
{
?>
</font>
</p>
<table border="0" cellspacing="1" cellpadding="2" width="100%" id="table8">

<tr>
<td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size:14px">Họ và tên :</font></td>
<td width="8">&nbsp; </td>
<td width="897">
	<font face="Tahoma">
	<span style="font-size: 14px">
	<b>
	<?=$cust['name']?></b></span></font><b><font color="#000000" style="font-size: 14px">
</font>
	</b>
</td>
</tr>
<tr>
<td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size:14px">Giới tính :</font></td>
<td width="8">&nbsp;</td>
<td>
                                <font face="Tahoma">
                                <span style="font-size:14px">
                                <b>
                                <?=$cust['sex']?></b></span></font><b><font color="#000000" style="font-size:14px">
</font>
								</b>
</td>
</tr>


<tr>
<td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size:14px">Địa chỉ :</font></td>
<td width="8">&nbsp;</td>
<td>
                                <font face="Tahoma">
                                <span style="font-size: 14px">
                                <b>
                                <?=$cust['address']?></b></span></font><b><font color="#000000" style="font-size: 14px">
</font>
								</b>
</td>
</tr>
<tr height="22">
<td width="211" align="right"><font color="#000000" style="font-size:14px" face="Tahoma">Điện thoại :</font></td> 
<td width="8">&nbsp; </td>
<td>
                                <font face="Tahoma">
                                <span style="font-size: 14px; font-weight:700">
                                <?=$cust['phone']?></span></font></td>
</tr>

<tr valign="middle" height="22">
<td width="211" align="right"><font color="#000000" style="font-size: 14px" face="Tahoma">Email :</font></td>
<td width="8">
</td>
<td>
<font color="#000000" style="font-size: 14px"> <b> <?=$cust['email']?></b></font></td>
</tr>

<tr>
<td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size: 14px">Tiêu đề:</font></td>
<td width="8">&nbsp; </td>
<td><font face="Tahoma"><span style="font-size: 14px">
                                <b>
                                <?=$cust['title']?></b></span></font><b><font color="#000000" style="font-size: 14px">
</font>
								</b>
</td>
</tr>
<tr>
<td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size: 14px">Nội dung thông điệp:</font></td>
<td width="8">&nbsp;</td>
<td>
    <font face="Tahoma">
     <span style="font-size: 12px">
 	  <b> <?=$cust['content']?></b></span></font><b><font color="#000000" style="font-size: 14px"></font></b>								
</td>
</tr>
<tr>
<td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size: 14px">Thời điểm giao hàng:</font></td>
<td width="8">&nbsp;</td>
<td>
    <font face="Tahoma">
     <span style="font-size: 14px">
 	  <b> <?=$cust['date']?></b></span></font><b><font color="#000000" style="font-size: 14px"></font></b>								
</td>
</tr>
<tr>
<td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size: 14px">Địa chỉ giao hàng:</font></td>
<td width="8">&nbsp;</td>
<td>
    <font face="Tahoma">
     <span style="font-size: 14px">
 	  <b> <?=$cust['add_to']?></b></span></font><b><font color="#000000" style="font-size: 14px"></font></b>								
</td>
</tr>
<tr>
  <td width="211" align="right"><font color="#000000" face="Tahoma" style="font-size: 14px">Thời gian giao hàng:</font> </td>
  <td>&nbsp;</td>
  <td><font face="Tahoma"><b>
    <?=$cust['time']?>
  </b></font></td>
</tr>
</table>
<br />

<table border="0" cellpadding="10" cellspacing="1" width="100%">
<tr><td class="DialogBox">
<form action="./" method="POST" name="cartform">
<table border="1" width="100%" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bordercolor="#333333">
	<tr>
		<td align="center" width="100" height="30"><h2>Hình ảnh</h2></td>
		<td width="400" height="34" align="center"><h2><font face="Tahoma" style="font-size: 12px">Sản phẩm</font></h2></td>
		<td align="center" width="100" height="30"><h2><font face="Tahoma" style="font-size: 12px">Số lượng</font></h2></td>
		<td align="center" width="160" height="30"><h2><font face="Tahoma" style="font-size: 12px">Đơn giá</font>
		  <span style="font-family:Tahoma; font-weight:600; color:#FF0000">
		    <?=$currencyUnit?>
		    </span></h2></td>
		<td align="center" width="164" height="30"><h2><font face="Tahoma" style="font-size: 12px">Thành tiền</font>
		  <span style="font-family:Tahoma; font-weight:600; color:#FF0000">
		    <?=$currencyUnit?>
		    </span>
		  </h2></td>
	</tr>
<?
$cart=$_SESSION['cart'];
$cnt=0;
$tongcong=0;
foreach ($cart as $product){
$sql = "select * from tbl_product where id='".$product[0]."'";
$result = mysql_query($sql,$conn);
if (mysql_num_rows($result)>0)
{
$pro=mysql_fetch_assoc($result);
?>	
	<tr>
		<td align="center" width="100">
		<font face="Tahoma" style="font-size: 12px">
<A href="./?frame=product_detail&id=<?=$pro['id']?>"><img src="<?=$pro['image']?>" alt="<?=$pro['name']?>" border="0" width="100"></A></font></td>
		<td align="center"><span style="font-size: 12px">
		  <?=$pro['name']?>
		</span></td>
		<td align="center" width="100"><span style="font-size: 12px"><?=$product[1]?></span></td>
		<td align="center" width="160"><span style="font-size: 12px"><?=number_format($pro['price'],0,',','.')?></span> </td>
		<td align="center" width="164"><span style="font-size: 12px"><?=number_format(($pro['price']*$product[1]),0,',','.')?>
		</span></td>
	</tr>
<?
}
$so_sanpham=$so_sanpham+$product[1];
$tongcong=$tongcong+$pro['price']*$product[1];
$cnt=$cnt+1;
} 
?>
</table>

<table border="0" width="100%">
<tr><td height="10px"></td></tr>
<tr><td align="right" style="padding-right:10px">
		<font face="Tahoma" style="font-size: 12px"><b>Tổng cộng : <?=number_format($tongcong,0,',','.')?></b></font>
			<span style="font-family:Tahoma; font-weight:600; color:#FF0000"><?=$currencyUnit?></span>
	</td></tr>
</table>
<HR align="left" noshade size="1">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table5">
<tr>
<td>
<p align="center"><font face="Verdana" size="1">
<input type="submit" class="buttonorange" onmouseover="this.className='buttonblue'" onmouseout="this.className='buttonorange'" style="height: 22px; width:130px" name="butSub" value="Gửi đơn hàng">
</font></td>
</tr>
</table>
<input type="hidden" name="frame" value="checkout">
</form>
</td></tr>
</table>
<?
}
?>